################################################
# Analysis for:
#
# Brown, Mitchell, Kathleen Hale, Soren Jordan, and Ryan D. Williamson. "Restoring 
#  Trust in US Elections through Effective Election Administrator Messaging." 
#  Puiblic Opinion Quarterly. 
#
# Required files: Trust survey experiment small.csv (analysis in paper)
#                 Trust survey experiment full.csv (before listwise deletion)
#
################################################

library(tidyverse)
library(stargazer)
library(ltm)
library(ggpubr)
library(plm)

data.complete <- read_csv("/Users/scj0014/Library/CloudStorage/Box-Box/Trust Project/Reports and Papers/POQ/Final/Analysis/Trust survey experiment small.csv")

summary(data.complete)
names(data.complete)

## Create conditions for messenger, message, and messenger + message (m_m)

data.complete$messenger <- factor(data.complete$messenger, levels = c("Control", "Black", "White"))
data.complete$m_m <- factor(data.complete$m_m, levels = c("Control", 
					"Black_Duty", "Black_Experience", "Black_Neighbor", "Black_Rules",
					"White_Duty", "White_Experience", "White_Neighbor", "White_Rules"))
					
## Create explicit factor conditions for Respondent race
data.complete$race <- factor(data.complete$race, levels = c("Other", "Black", "White"))

## Create change in levels of trust as well as total pre/post trust
data.complete$change_leo_followrule_num <- data.complete$post_leo_followrule_num - data.complete$pre_leo_followrule_num
data.complete$change_leo_freefair_num <- data.complete$post_leo_freefair_num - data.complete$pre_leo_freefair_num
data.complete$change_leo_ethical_num <- data.complete$post_leo_ethical_num - data.complete$pre_leo_ethical_num

data.complete$total_pre_trust_num <- data.complete$pre_leo_followrule_num + data.complete$pre_leo_freefair_num + data.complete$pre_leo_ethical_num
data.complete$total_post_trust_num <- data.complete$post_leo_followrule_num + data.complete$post_leo_freefair_num + data.complete$post_leo_ethical_num

data.complete$total_change_trust_num <- data.complete$total_post_trust_num - data.complete$total_pre_trust_num

## Identify low trusters (initial)
data.complete$low_truster <- ifelse(data.complete$total_pre_trust_num < quantile(data.complete$total_pre_trust_num, na.rm = TRUE)[2], 1, 0)
table(data.complete$low_truster, data.complete$total_pre_trust_num)



data.pre.trust <- data.frame(data.complete$pre_leo_followrule_num, data.complete$pre_leo_freefair_num, data.complete$pre_leo_ethical_num)
cronbach.alpha(data.pre.trust)
factanal(data.pre.trust, factors = 1)

data.post.trust <- data.frame(data.complete$post_leo_followrule_num, data.complete$post_leo_freefair_num, data.complete$post_leo_ethical_num)
cronbach.alpha(data.post.trust)
factanal(data.post.trust, factors = 1)








######## Two simple descriptives first.

## What does baseline trust look like?
# THIS IS FIGURE 2
# pdf("/Users/scj0014/Library/CloudStorage/Box-Box/Trust Project/Reports and Papers/POQ/Final/final-to-poq/figure2_pretrust.pdf", width = 7, height = 7)
ggplot(data = data.complete, aes(x = total_pre_trust_num)) + 
	geom_density(alpha = .2, adjust = 2) +
	facet_wrap(vars(race), nrow = 3) + 
	labs(x = "Combined Baseline Trust in Local Election Officials (LEOs)\nby Respondent Race, Before Treatment", y = "Density") +
	theme_bw()
dev.off()

table(data.complete$race)






#######################################
############## Let's predict changes in each dimension of trust
#######################################

### First: establish levels of controls for Figures. These will be used to make predictions for plotting.
#    They are the same in each regression.

# Same controls in each regression
predict.data.controls <- expand.grid(low_truster = c(0, 1), 
						race = levels(as.factor(data.complete$race)),
						m_m = (levels(as.factor(data.complete$m_m))[-1]), # omit fitting the control
						interest_num = mean(data.complete$interest_num),
						pid_clean = mean(data.complete$pid_clean),
						ideology_num = mean(data.complete$ideology_num),
						income_num = mean(data.complete$income_num),
						voted_num = 1,
						education_num = mean(data.complete$education_num),
						age_num_clean = mean(data.complete$age_num_clean),
						knowledge = 0,
						female_num = 0,
						conspiratorial = mean(data.complete$conspiratorial))

################
## Now, for each dimension
################

##### 1. Free and fair elections. #####

# Baseline model (just experimental treatments). This is Table 2, column 1
model.freefair <- lm(change_leo_freefair_num ~ m_m, data = data.complete)
summary(model.freefair)

# With controls
model_controls.freefair <- lm(change_leo_freefair_num ~ m_m*race*low_truster +
		interest_num + pid_clean + ideology_num + income_num + voted_num +
		education_num + age_num_clean + knowledge + conspiratorial +
		female_num, data = data.complete)

summary(model_controls.freefair)


## Plot as difference-in-difference

predictions.data.controls.freefair <- predict(model_controls.freefair, predict.data.controls, se.fit = TRUE)

plot.predict.data.controls.freefair <- data.frame(Change = predictions.data.controls.freefair$fit,
						Change.upper = predictions.data.controls.freefair$fit + predictions.data.controls.freefair$se.fit*1.96,
						Change.lower = predictions.data.controls.freefair$fit - predictions.data.controls.freefair$se.fit*1.96,
						Race = predict.data.controls$race,
						Condition = predict.data.controls$m_m,
						Trust = factor(ifelse(predict.data.controls$low_truster == 1, "Low", "High")))

# Grouping level for plot facets
plot.predict.data.controls.freefair$Messenger <- ifelse(as.numeric(plot.predict.data.controls.freefair$Condition) %in% 1:4, 
													"Messenger: Black Male",
													"Messenger: White Female")

table(plot.predict.data.controls.freefair$Messenger , plot.predict.data.controls.freefair$Condition)


plot.predict.data.controls.freefair$manual.placement <- c(rep(1, 6), rep(2, 6), rep(3, 6), rep(4, 6)) + (as.numeric(plot.predict.data.controls.freefair$Race) - 2)/3

plot.predict.data.controls.freefair$adjuster <- ifelse(predict.data.controls$low_truster == 1, 0.03, -0.03)
plot.predict.data.controls.freefair$manual.placement <- plot.predict.data.controls.freefair$manual.placement + 	
												plot.predict.data.controls.freefair$adjuster 

table(plot.predict.data.controls.freefair$manual.placement, plot.predict.data.controls.freefair$Condition)


# THIS IS FIGURE 3
# pdf("/Users/scj0014/Library/CloudStorage/Box-Box/Trust Project/Reports and Papers/POQ/Final/final-to-poq/figure3_freefair.pdf", width = 7, height = 7)
ggplot(data = plot.predict.data.controls.freefair, aes(x = manual.placement, y = Change, pch = Race, color = Trust)) + 
	geom_point(size = 3) + 
	geom_vline(xintercept = seq(1.5, 3.5, 1), lwd = 2) +
	geom_errorbar(aes(ymin = Change.lower, ymax = Change.upper), lwd = 0.5) +	
	theme_bw() +
	facet_wrap(~Messenger, nrow = 2) +
	scale_colour_grey(start = 0.7, end = 0.1) + 
	scale_x_continuous(breaks = 1:4, labels = c("Message:\nCivic Duty", 
			"Message:\nExperience", 
			"Message:\nNeighbor", 
			"Message:\nFollow Rules")) + 
	labs(x = "", y = "Difference in Difference: Change in Trust in LEO Free and Fair\n(Compared to Change in Control)")
dev.off()




##### 2. Following the rule of law. #####

# Baseline model (just experimental treatments). This is Table 2, column 2
model.followrule <- lm(change_leo_followrule_num ~ m_m, data = data.complete)

summary(model.followrule)

# With controls
model_controls.followrule <- lm(change_leo_followrule_num ~ m_m*race*low_truster +
		interest_num + pid_clean + ideology_num + income_num + voted_num +
		education_num + age_num_clean + knowledge + conspiratorial +
		female_num - 1, data = data.complete)

summary(model_controls.followrule)


## Plot as difference-in-difference

predictions.data.controls.followrule <- predict(model_controls.followrule, predict.data.controls, se.fit = TRUE)

plot.predict.data.controls.followrule <- data.frame(Change = predictions.data.controls.followrule$fit,
						Change.upper = predictions.data.controls.followrule$fit + predictions.data.controls.followrule$se.fit*1.96,
						Change.lower = predictions.data.controls.followrule$fit - predictions.data.controls.followrule$se.fit*1.96,
						Race = predict.data.controls$race,
						Condition = predict.data.controls$m_m,
						Trust = factor(ifelse(predict.data.controls$low_truster == 1, "Low", "High")))

# Grouping level for plot facets
plot.predict.data.controls.followrule$Messenger <- ifelse(as.numeric(plot.predict.data.controls.followrule$Condition) %in% 1:4, 
													"Messenger: Black Male",
													"Messenger: White Female")

table(plot.predict.data.controls.followrule$Messenger , plot.predict.data.controls.followrule$Condition)


plot.predict.data.controls.followrule$manual.placement <- c(rep(1, 6), rep(2, 6), rep(3, 6), rep(4, 6)) + (as.numeric(plot.predict.data.controls.followrule$Race) - 2)/3

plot.predict.data.controls.followrule$adjuster <- ifelse(predict.data.controls$low_truster == 1, 0.03, -0.03)
plot.predict.data.controls.followrule$manual.placement <- plot.predict.data.controls.followrule$manual.placement + 	
												plot.predict.data.controls.followrule$adjuster 

table(plot.predict.data.controls.followrule$manual.placement, plot.predict.data.controls.followrule$Condition)


# THIS IS FIGURE 4
# pdf("/Users/scj0014/Library/CloudStorage/Box-Box/Trust Project/Reports and Papers/POQ/Final/final-to-poq/figure4_followrule.pdf", width = 7, height = 7)
ggplot(data = plot.predict.data.controls.followrule, aes(x = manual.placement, y = Change, pch = Race, color = Trust)) + 
	geom_point(size = 3) + 
	geom_vline(xintercept = seq(1.5, 3.5, 1), lwd = 2) +
	geom_errorbar(aes(ymin = Change.lower, ymax = Change.upper), lwd = 0.5) +	
	theme_bw() +
	facet_wrap(~Messenger, nrow = 2) +
	scale_colour_grey(start = 0.7, end = 0.1) + 
	scale_x_continuous(breaks = 1:4, labels = c("Message:\nCivic Duty", 
			"Message:\nExperience", 
			"Message:\nNeighbor", 
			"Message:\nFollow Rules")) + 
	labs(x = "", y = "Difference in Difference: Change in Trust in LEO Follow Rules\n(Compared to Change in Control)")
dev.off()




##### 3. LEO are ethical. #####

# Baseline model (just experimental treatments). This is Table 2, column 3
model.ethical <- lm(change_leo_ethical_num ~ m_m, data = data.complete)

summary(model.ethical)

# With controls
model_controls.ethical <- lm(change_leo_ethical_num ~ m_m*race*low_truster +
		interest_num + pid_clean + ideology_num + income_num + voted_num +
		education_num + age_num_clean + knowledge + conspiratorial +
		female_num, data = data.complete)

summary(model_controls.ethical)


## Plot as difference-in-difference

predictions.data.controls.ethical <- predict(model_controls.ethical, predict.data.controls, se.fit = TRUE)

plot.predict.data.controls.ethical <- data.frame(Change = predictions.data.controls.ethical$fit,
						Change.upper = predictions.data.controls.ethical$fit + predictions.data.controls.ethical$se.fit*1.96,
						Change.lower = predictions.data.controls.ethical$fit - predictions.data.controls.ethical$se.fit*1.96,
						Race = predict.data.controls$race,
						Condition = predict.data.controls$m_m,
						Trust = factor(ifelse(predict.data.controls$low_truster == 1, "Low", "High")))

# Grouping level for plot facets
plot.predict.data.controls.ethical$Messenger <- ifelse(as.numeric(plot.predict.data.controls.ethical$Condition) %in% 1:4, 
													"Messenger: Black Male",
													"Messenger: White Female")

table(plot.predict.data.controls.ethical$Messenger , plot.predict.data.controls.ethical$Condition)


plot.predict.data.controls.ethical$manual.placement <- c(rep(1, 6), rep(2, 6), rep(3, 6), rep(4, 6)) + (as.numeric(plot.predict.data.controls.ethical$Race) - 2)/3

plot.predict.data.controls.ethical$adjuster <- ifelse(predict.data.controls$low_truster == 1, 0.03, -0.03)
plot.predict.data.controls.ethical$manual.placement <- plot.predict.data.controls.ethical$manual.placement + 	
												plot.predict.data.controls.ethical$adjuster 

table(plot.predict.data.controls.ethical$manual.placement, plot.predict.data.controls.ethical$Condition)


# THIS IS FIGURE 5
# pdf("/Users/scj0014/Library/CloudStorage/Box-Box/Trust Project/Reports and Papers/POQ/Final/final-to-poq/figure5_ethical.pdf", width = 7, height = 7)
ggplot(data = plot.predict.data.controls.ethical, aes(x = manual.placement, y = Change, pch = Race, color = Trust)) + 
	geom_point(size = 3) + 
	geom_vline(xintercept = seq(1.5, 3.5, 1), lwd = 2) +
	geom_errorbar(aes(ymin = Change.lower, ymax = Change.upper), lwd = 0.5) +	
	theme_bw() +
	facet_wrap(~Messenger, nrow = 2) +
	scale_colour_grey(start = 0.7, end = 0.1) + 
	scale_x_continuous(breaks = 1:4, labels = c("Message:\nCivic Duty", 
			"Message:\nExperience", 
			"Message:\nNeighbor", 
			"Message:\nFollow Rules")) + 
	labs(x = "", y = "Difference in Difference: Change in Trust in LEO Ethical\n(Compared to Change in Control)")
dev.off()















#################################################################################################################################################
# Analysis for SM

# n of each group (table SM1)
data.complete %>% 
	group_by(m_m, low_truster, race) %>%
	count() %>% 
	pivot_wider(names_from = race, values_from = n)







# Compare pre/post listwise deletion (table SM2)

data.full <- read_csv("/Users/scj0014/Library/CloudStorage/Box-Box/Trust Project/Reports and Papers/POQ/Final/Analysis/Trust survey experiment full.csv")

## Add the same change variables and low_trust variables to the full dataset
# Create change in levels of trust as well as total pre/post trust
data.full$change_leo_followrule_num <- data.full$post_leo_followrule_num - data.full$pre_leo_followrule_num
data.full$change_leo_freefair_num <- data.full$post_leo_freefair_num - data.full$pre_leo_freefair_num
data.full$change_leo_ethical_num <- data.full$post_leo_ethical_num - data.full$pre_leo_ethical_num

# Identify low trusters (initial)
data.full$total_pre_trust_num <- data.full$pre_leo_followrule_num + data.full$pre_leo_freefair_num + data.full$pre_leo_ethical_num
data.full$low_truster <- ifelse(data.full$total_pre_trust_num < quantile(data.full$total_pre_trust_num, na.rm = TRUE)[2], 1, 0)

vars.to.summarize <- c("pre_leo_followrule_num", "pre_leo_freefair_num", "pre_leo_ethical_num", 
	"post_leo_followrule_num", "post_leo_freefair_num", "post_leo_ethical_num", 
	"change_leo_followrule_num", "change_leo_freefair_num", "change_leo_ethical_num", 
	"low_truster", "interest_num", "pid_clean", "ideology_num", "income_num", "voted_num",
	"education_num", "age_num_clean", "senator_correct", "budget_correct", "knowledge", 
	"white", "black", "other", "female_num", 
	"consp_1_plots", "consp_2_plots", "consp_3_plots", "consp_4_plots", "conspiratorial", 
	"manip_check", "attn_check")

after.summary <- data.complete %>% 
    dplyr::select(all_of(vars.to.summarize)) %>%
    pivot_longer(everything()) %>% 
    group_by(name) %>% 
    summarise(
        mean = mean(value, na.rm = TRUE),
        sd = sd(value, na.rm = TRUE),
        n = table(is.na(value))[1]
    )

before.summary <- data.full %>% 
    dplyr::select(all_of(vars.to.summarize)) %>%
    pivot_longer(everything()) %>% 
    group_by(name) %>% 
    summarise(
        mean = mean(value, na.rm = TRUE),
        sd = sd(value, na.rm = TRUE),
        n = table(is.na(value))[1]
    )

cbind(before.summary[,1], round(before.summary[,2:4], digits = 3), round(after.summary[,2:4], digits = 3))

